package com.coco.mapper;

import com.coco.pojo.LeaveType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 请假类型Mapper接口
 */
@Mapper
public interface LeaveTypeMapper {

    /**
     * 插入新的请假类型记录
     * @param leaveType 请假类型对象
     * @return 受影响的行数
     */
    @Insert("INSERT INTO leave_types(type_name, description, applicable_conditions) " +
            "VALUES(#{typeName}, #{description}, #{applicableConditions})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(LeaveType leaveType);

    /**
     * 根据ID更新请假类型信息
     * @param leaveType 请假类型对象
     * @return 受影响的行数
     */
    @Update("UPDATE leave_types SET type_name=#{typeName}, description=#{description}, " +
            "applicable_conditions=#{applicableConditions} WHERE id=#{id}")
    int updateById(LeaveType leaveType);

    /**
     * 根据ID删除请假类型记录
     * @param id 请假类型ID
     * @return 受影响的行数
     */
    @Delete("DELETE FROM leave_types WHERE id=#{id}")
    int deleteById(Integer id);

    /**
     * 根据ID查询请假类型信息
     * @param id 请假类型ID
     * @return 请假类型对象
     */
    @Select("SELECT * FROM leave_types WHERE id=#{id}")
    @Results(id = "leaveTypeMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "type_name", property = "typeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "applicable_conditions", property = "applicableConditions", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_at", property = "createdAt", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updated_at", property = "updatedAt", jdbcType = JdbcType.TIMESTAMP)
    })
    LeaveType selectById(Integer id);

    /**
     * 查询所有请假类型信息
     * @return 请假类型对象列表
     */
    @Select("SELECT * FROM leave_types")
    @ResultMap("leaveTypeMap")
    List<LeaveType> selectAll();

    /**
     * 根据条件查询请假类型信息
     * @param leaveType 查询条件
     * @return 请假类型对象列表
     */
    @Select("<script>" +
            "SELECT * FROM leave_types" +
            "<where>" +
            "    <if test='typeName != null and typeName != \"\"'>" +
            "        AND type_name LIKE CONCAT('%', #{typeName}, '%')" +
            "    </if>" +
            "    <if test='description != null and description != \"\"'>" +
            "        AND description LIKE CONCAT('%', #{description}, '%')" +
            "    </if>" +
            "    <if test='applicableConditions != null and applicableConditions != \"\"'>" +
            "        AND applicable_conditions LIKE CONCAT('%', #{applicableConditions}, '%')" +
            "    </if>" +
            "</where>" +
            "</script>")
    @ResultMap("leaveTypeMap")
    List<LeaveType> selectByEntity(LeaveType leaveType);
}